package com.myway.dto.flight;

import com.myway.dto.flight.domain.TripType;
import com.myway.dto.order.domain.BookingType;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 返回的航班对象 对于中转航班才会出现多条航段，请注意使用
 * @author guoguo
 * @version 1.0
 * @created 12-五月-2008 14:42:07
 */
public class DTOFlight implements Serializable, Cloneable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8798687081411197180L;
	/**
	 * 航段集合
	 */
	private List<DTOSegment> segments=new ArrayList<DTOSegment>();
	
	/**
	 * 团队或散客预定
	 */
	private BookingType bookingType = BookingType.INDIVIDUAL;
	
	/**
	 * 旅行类型
	 */
	private TripType tripType=TripType.ONEWAY;
	
	/**
	 * 航线类型  是否为包机航线
	 */
	private String charterPlane = "no";
	
	/**
	 * 成人总数
	 */
	private int adultCount;
	
	/**
	 * 儿童总数 
	 */
	private int childCount;
	
	/**
	 * 婴儿总数
	 */
	private int infantCount;
	
	private boolean searchMilePoints=false;

	public DTOFlight(){

	}
	
	public Object clone() throws CloneNotSupportedException {
		return super.clone();
	}

	/**
	 * Return total flight time in milliseconds
	 * @param flt
	 * @return
	 */
	public long getTotalFlightTime() {
		if (segments == null || segments.size() > 0) {
			return 0;
		} else {
			DTOSegment firstSegment;
			DTOSegment lastSegment;
			
			// Sort segment list by departure time
			Collections.sort(segments);
			
			firstSegment = segments.get(0);
			lastSegment = segments.get(segments.size() - 1);
			
			long start = firstSegment.getDepartureTime().getTime();
			long end = lastSegment.getArriveTime().getTime();
			
			return end - start;
		}
	}

	/**
	 * Return total waiting time in milliseconds. Total waiting time is the accumulation of time between the
	 * arrival time and the departure time of next flight segment. If the flight has only 1 segment, the total
	 * waiting time will always be zero.
	 * 
	 * @return Total waiting time.
	 */
	public long getTotalWaitingTime() {
		long totalWaitingTime = 0L;

		for (int i = 0; i < segments.size() - 1; i++) {
			DTOSegment s1 = segments.get(i);
			DTOSegment s2 = segments.get(i+1);
			totalWaitingTime += s2.getDepartureTime().getTime() - s1.getArriveTime().getTime();
		}
		
		return totalWaitingTime;
	}
	
	/**
	 * Get the first departure airport code of this flight.
	 * 
	 * @return Airport code of the first destination
	 */
	public String getFirstDeparturePort() {
		String port = null;
		if (segments != null && segments.size() > 0) {
			port = segments.get(0).getOriginCity();
		}
		return port;
	}

	/**
	 * Get the final destination airport code of this flight.
	 * 
	 * @return Airport code of the final destination
	 */
	public String getFinalDestinationPort() {
		String port = null;
		if (segments != null && segments.size() > 0) {
			port = segments.get(segments.size()-1).getDestinationCity();
		}
		return port;
	}
	
	/**
	 * @return the segments
	 */
	public List<DTOSegment> getSegments() {
		return this.segments;
	}

	/**
	 * @param segments the segments to set
	 */
	public void setSegments(List<DTOSegment> segments) {
		this.segments = segments;
	}

	public void addDTOSegment(DTOSegment DTOSegment){
		segments.add(DTOSegment);
	}
	
	public BookingType getBookingType() {
		return bookingType;
	}

	public void setBookingType(BookingType bookingType) {
		this.bookingType = bookingType;
	}

	public int getAdultCount() {
		return adultCount;
	}

	public void setAdultCount(int adultCount) {
		this.adultCount = adultCount;
	}

	public int getChildCount() {
		return childCount;
	}

	public void setChildCount(int childCount) {
		this.childCount = childCount;
	}

	public int getInfantCount() {
		return infantCount;
	}

	public void setInfantCount(int infantCount) {
		this.infantCount = infantCount;
	}

	/**
	 * @return the tripType
	 */
	public TripType getTripType() {
		return this.tripType;
	}

	/**
	 * @param tripType the tripType to set
	 */
	public void setTripType(TripType tripType) {
		this.tripType = tripType;
	}

	public boolean isSearchMilePoints() {
		return searchMilePoints;
	}

	public void setSearchMilePoints(boolean searchMilePoints) {
		this.searchMilePoints = searchMilePoints;
	}

	public String getCharterPlane() {
		return charterPlane;
	}

	public void setCharterPlane(String charterPlane) {
		this.charterPlane = charterPlane;
	}
	
	
	
	

}